<?php

namespace Admin\Controller;

use User\Api\UserApi;

class FleetController extends AdminController {

    public function index() {

        $keywords = I('keywords');
        $where = '1=1 ';
        if ($keywords) {
            $where = "and ( fleet_name like '%" . $keywords . "%' or fleet_linkman like '%" . $keywords . "%' or fleet_phone = " . $keywords . ") ";
            $this->assign('keywords', $keywords);
        }

        $M = M()->table("ot_fleet as f")
                ->join('ot_ucenter_member AS um on f.uid = um.id');
        $field = 'f.*,um.status';
        $order = "f.fleet_id desc";
        $list = $this->lists($M, $where, $order, $field);
        
        $this->assign('_list', $list);
        $this->display();
    }

    public function changestatus() {
        $uid = I('id', 0, 'intval');
        if ($uid == 1) {
            $this->error('超级管理员账号不允许禁用');
        }
        $res = 0;
        $status = I('status', -1, 'intval');
        $repwd = I('repwd', 0, 'intval');
        if ($uid) {
            if ($status != -1) {
                if ($status == 0) {
                    //禁用
                    $res1 = M('Fleet')->where(array('uid' => $uid))->save(['fleet_status' => 2]);
                }else{
                    //启用
                    $res1 = M('Fleet')->where(array('uid' => $uid))->save(['fleet_status' => 1]);
                }
                $data['status'] = $status;
                $res = M('UcenterMember')->where(array('id' => $uid))->save($data);
            }
            if ($repwd != 0) {
                $udata = array('password' => '123456');
                $User = new UserApi;
                $res = $User->updateInfoNoVerify($uid, $udata);
                if (!$res['status']) {
                    if ($res['info'] == -4) {
                        $this->error('密码长度不合法');
                    }
                } else {
                    $res = 1;
                }
            }
        }

        if ($res) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }

    public function add() {
        $id = I('id', 0, 'intval');

        if (IS_POST) {

            $fleet_name = I('fleet_name');
            $fleet_linkman = I('fleet_linkman');
            $fleet_phone = I('fleet_phone');
            $uid = I('uid', 0, 'intval');

            $username = I('username');
            if (empty($fleet_name)) {
                $this->error('车队名必填');
            }

            if (empty($fleet_linkman)) {
                $this->error('车队联系人必填');
            }
            if (empty($fleet_phone)) {
                $this->error('车队联系电话必填');
            }
            if (empty($fleet_phone)) {
                $this->error('车队联系电话必填');
            }
            if (empty($username)) {
                $this->error('车队登录账号必填');
            }

            $data['fleet_name'] = $fleet_name;
            $data['fleet_linkman'] = $fleet_linkman;
            $data['fleet_phone'] = $fleet_phone;
            $data['fleet_login_name'] = $username;

            $M = M('Fleet');
            $M->startTrans();

            if ($id != 0) {

                $res = M('Fleet')->where(['fleet_id' => $id])->save($data);

                if (!$res) {
                    $M->rollback();
                    $this->error('修改失败！');
                }

                $User = new UserApi;
                $udata = array('username' => $username, 'mobile' => $fleet_phone);
                $user = array('nickname' => $username);

                $res = $User->updateInfoNoVerify($uid, $udata);
                M('Member')->where('uid=' . $uid)->save($user);
                if (!$res['status']) {
                    $M->rollback();
                    if ($res['info'] == -11) {
                        $this->error('手机号已经被占用');
                    }

                    if ($res['info'] == -3) {
                        $this->error('登录账号被占用');
                    }
                }

                //成功
                $M->commit();
                $this->success('修改成功!', U('index'));
            } else {

                /* 调用注册接口注册用户 */
                $User = new UserApi;
                $uid = $User->register($username, '123456', $fleet_phone . '@test.com', $fleet_phone);

                if (0 < $uid) {
                    $user = array('uid' => $uid, 'nickname' => $fleet_linkman, 'status' => 1);
                    if (!M('Member')->add($user)) {
                        $M->rollback();
                        $this->error('用户添加失败！');
                    }
                    $data['uid'] = $uid;
                    $res = M('Fleet')->add($data);

                    if (!$res) {
                        $M->rollback();
                        $this->error('新增失败！');
                    }

                    /* 授权用户 */

                    $AuthGroup = D('AuthGroup');

                    $awhere['title'] = array('LIKE', '%车队%');
                    $gid = $AuthGroup->where($awhere)->getField('id');
                    $adata['uid'] = $uid;
                    $adata['group_id'] = $gid;
                    $gres = M('AuthGroupAccess')->add($adata);
                    if ($gres) {
                        //成功
                        $M->commit();
                        $this->success('添加成功!', U('index'));
                    } else {
                        $this->error($AuthGroup->getError());
                    }
                } else { //注册失败，显示错误信息
                    $M->rollback();

                    if ($uid == -11) {
                        $this->error('手机号已经被占用');
                    }

                    if ($uid == -3) {
                        $this->error('登录账号被占用');
                    }
                    $this->error('添加失败!');
                }
            }
        } else {

            $data = M('Fleet')->where(['fleet_id' => $id])->find();
            $this->assign('data', $data);
            $this->display();
        }
    }

}
